//
//  SunRecordCell.m
//  Xspcf
//
//  Created by sunshine on 16/10/31.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunRecordCell.h"
#import "SunTriangle.h"
@interface SunRecordCell()
{
    NSDictionary *_dic;
}
// 时间hh:mm
@property (nonatomic, strong) UILabel *timeLabel;
// 结果
@property (nonatomic, strong) UILabel *resultLabel;
// 描述
@property (nonatomic, strong) UILabel *descLabel;
// 金额
@property (nonatomic, strong) UILabel *moneyLabel;

@property (nonatomic, strong) UIButton *cancelButton;

@end
@implementation SunRecordCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView type:(int)type{
    static NSString *ID = @"cellID";
    SunRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[SunRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell creatSubviews:type];
    }
    return cell;
}

- (void)creatSubviews:(int)type{
    
    self.dateLable = [[UILabel alloc]init];
    [self addSubview:self.dateLable];
    [self.dateLable setFrame:CGRectMake(X(11), 0, X(40), 37)];
    self.dateLable.font = [UIFont fontWithName:@"PingFangSC-Medium" size:26];
    self.dateLable.textColor = [UIColor blackColor];
    self.dateLable.hidden = YES;
    
    self.monthLabel = [[UILabel alloc]init];
    [self addSubview:self.monthLabel];
    [self.monthLabel setFrame:CGRectMake(X(42), 15, X(30), 17)];
    self.monthLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
    self.monthLabel.textColor = [UIColor blackColor];
    self.monthLabel.hidden = YES;
    
    self.timeLabel = [[UILabel alloc]init];
    [self addSubview:self.timeLabel];
    [self.timeLabel setFrame:CGRectMake(X(28), 34, X(35), 16)];
    self.timeLabel.font = [UIFont systemFontOfSize:11];
    self.timeLabel.textColor = [UIColor colorWithHexString:@"8B8C8D"];
    
    UILabel *lineLabel = [[UILabel alloc]init];
    [self addSubview:lineLabel];
    [lineLabel setFrame:CGRectMake(X(68), 0, 1, 81)];
    lineLabel.backgroundColor = [UIColor colorWithHexString:@"CDCDCD"];
    
    UILabel *circleLabel = [[UILabel alloc]init];
    [self addSubview:circleLabel];
    [circleLabel setFrame:CGRectMake(X(64), 36, 12, 12)];
    circleLabel.backgroundColor = [UIColor colorWithHexString:@"CDCDCD"];
    circleLabel.layer.masksToBounds = YES;
    circleLabel.layer.cornerRadius = 6;
    
    self.resultLabel = [[UILabel alloc]init];
    [self addSubview:self.resultLabel];
    [self.resultLabel setFrame:CGRectMake(X(89), 28, X(200), 33)];
    self.resultLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:24];
    self.resultLabel.textColor = [UIColor colorWithHexString:@"000000"];

    self.descLabel = [[UILabel alloc]init];
    [self addSubview:self.descLabel];
    [self.descLabel setFrame:CGRectMake(X(89), 60, X(250), 20)];
    self.descLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
    self.descLabel.textColor = [UIColor colorWithHexString:@"ADADAD"];
    
    self.moneyLabel = [[UILabel alloc]init];
    [self addSubview:self.moneyLabel];
    [self.moneyLabel setFrame:CGRectMake(SCREENWIDTH - X(220), 28, X(200), 33)];
    self.moneyLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:24];
    self.moneyLabel.textColor = [UIColor colorWithHexString:@"D0021B"];
    self.moneyLabel.textAlignment = NSTextAlignmentRight;
    
    if (type == 1) {
        self.cancelButton = [[UIButton alloc]init];
        [self.cancelButton setFrame:CGRectMake(SCREENWIDTH - X(85), 61, X(65), 33)];
        [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"record_button"] forState:UIControlStateNormal];
        [self.cancelButton setTitle:@"取消提现" forState:UIControlStateNormal];
        [self.cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.cancelButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
        [self addSubview:self.cancelButton];
        [self.cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
        self.cancelButton.hidden = YES;
    }

}

- (void)setCellData:(NSDictionary *)data type:(int)type{
    _dic = data;
    if (type == 1) {
        NSString *timeString = [Sun timestamp:[[data objectForKey:@"time"] intValue] ToTime:@"YYYY-MM-dd HH:mm"];
        self.timeLabel.text = [timeString substringFromIndex:[timeString length] -5];
        self.resultLabel.text = [data objectForKey:@"status"];
        self.descLabel.text = [NSString stringWithFormat:@"提现至%@(尾号%@)",[data objectForKey:@"bank"],[data objectForKey:@"bankCard"]];
        self.moneyLabel.text = [NSString stringWithFormat:@"%.2f",[[data objectForKey:@"money"]doubleValue]];
        if ([[data objectForKey:@"status"] isEqualToString:@"充值失败"]) {
            self.resultLabel.textColor = [UIColor colorWithHexString:@"AFAFAF"];
            self.moneyLabel.textColor = [UIColor colorWithHexString:@"B7B7B7"];
        }
        if ([[data objectForKey:@"status"] isEqualToString:@"待审核"]) {
            self.cancelButton.hidden = NO;
        }else {
            self.cancelButton.hidden = YES;
        }
        if ([[data objectForKey:@"status"] isEqualToString:@"取消提现"]) {
            self.resultLabel.textColor = [UIColor colorWithHexString:@"AFAFAF"];
            self.moneyLabel.textColor = [UIColor colorWithHexString:@"B7B7B7"];
        }else {
            self.resultLabel.textColor = [UIColor colorWithHexString:@"000000"];
            self.moneyLabel.textColor = [UIColor colorWithHexString:@"D0021B"];
        }

    }else {
        NSString *timeString = [Sun timestamp:[[data objectForKey:@"time"] intValue] ToTime:@"YYYY-MM-dd HH:mm"];
        self.timeLabel.text = [timeString substringFromIndex:[timeString length] -5];
        self.resultLabel.text = [data objectForKey:@"status"];
        self.descLabel.text = [data objectForKey:@"desc"];
        self.moneyLabel.text = [NSString stringWithFormat:@"%.2f",[[data objectForKey:@"money"]doubleValue]];
        if ([[data objectForKey:@"status"] isEqualToString:@"充值失败"]) {
            self.resultLabel.textColor = [UIColor colorWithHexString:@"AFAFAF"];
            self.moneyLabel.textColor = [UIColor colorWithHexString:@"B7B7B7"];
        }else{
            self.resultLabel.textColor = [UIColor colorWithHexString:@"000000"];
            self.moneyLabel.textColor = [UIColor colorWithHexString:@"D0021B"];
        }
    }
    
}

- (void)cancel:(UIButton *)btn{
    NSString *params = [NSString stringWithFormat:@"id=%@",[_dic objectForKey:@"id"]];
    NSString *urlStr=[NSString stringWithFormat:@"%@%@",KURL,KAccount_Cash_Cancel];
    BOOL HaveAccessID = NO;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
        HaveAccessID = YES;
    }
    [HttpRequest postRequestCacheURLStr:urlStr withParameter:params HaveAccessID:HaveAccessID IsHTML:NO success:^(NSDictionary *requestDic, NSString *msg) {
        if ([requestDic[@"code"] intValue]==0) {
            btn.hidden = YES;
            self.resultLabel.text = @"提现申请取消";
            self.resultLabel.textColor = [UIColor colorWithHexString:@"AFAFAF"];
        }else{
            [[Sun shareSun]showMsg:@"撤回失败"];
        }
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
